package com.campuscarbooking.api.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campuscarbooking.api.model.Reservation;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface ReservationMapper extends BaseMapper<Reservation> {

    /**
     * 根据用户ID和预约状态查询预约信息
     */
    @Select("SELECT r.* FROM reservations r WHERE r.user_id = #{userId} AND r.status = #{status}")
    IPage<Reservation> findByUserIdAndStatus(Page<?> page, @Param("userId") Long userId, @Param("status") String status);

    /**
     * 根据用户ID和出发时间范围查询预约信息
     */
    @Select("SELECT r.* FROM reservations r JOIN bus_schedule bs ON r.schedule_id = bs.id " +
            "WHERE r.user_id = #{userId} AND bs.departure_time >= #{startTime} AND bs.departure_time < #{endTime}")
    List<Reservation> findByUserIdAndDepartureTime(@Param("userId") Long userId, @Param("startTime") LocalTime startTime, @Param("endTime") LocalTime endTime);

    /**
     * 根据班车ID查询有效预约数量
     */
    @Select("SELECT COUNT(*) FROM reservations r WHERE r.schedule_id = #{scheduleId} AND r.status != 'canceled'")
    Integer countActiveReservationsByScheduleId(@Param("scheduleId") Long scheduleId);

    /**
     * 根据班车ID查询有效预约，按用户类型排序（教师优先）
     */
    @Select("SELECT r.* FROM reservations r JOIN users u ON r.user_id = u.id " +
            "WHERE r.schedule_id = #{scheduleId} AND r.status != 'canceled' " +
            "ORDER BY CASE WHEN u.user_type = 'teacher' THEN 0 ELSE 1 END")
    List<Reservation> findActiveReservationsByScheduleIdOrderByUserType(@Param("scheduleId") Long scheduleId);

    /**
     * 根据出发时间范围查询预约信息
     */
    @Select("SELECT r.* FROM reservations r JOIN bus_schedule bs ON r.schedule_id = bs.id " +
            "WHERE bs.departure_time >= #{startTime} AND bs.departure_time < #{endTime}")
    List<Reservation> findByDepartureTimeBetween(@Param("startTime") LocalTime startTime, @Param("endTime") LocalTime endTime);

    /**
     * 根据出发时间范围分页查询预约信息
     */
    @Select("SELECT r.* FROM reservations r JOIN bus_schedule bs ON r.schedule_id = bs.id " +
            "WHERE bs.departure_time >= #{startTime} AND bs.departure_time < #{endTime}")
    IPage<Reservation> findByDepartureTimeBetween(Page<?> page, @Param("startTime") LocalTime startTime, @Param("endTime") LocalTime endTime);

    /**
     * 统计用户今日取消的预约数量
     */
    @Select("SELECT COUNT(*) FROM reservations r WHERE r.user_id = #{userId} AND r.status = 'canceled' AND r.canceled_at >= #{startOfDay}")
    int countCancelledReservationsToday(@Param("userId") Long userId, @Param("startOfDay") LocalDateTime startOfDay);

    /**
     * 根据预约ID查询预约详情（包含用户和班车信息）
     */
    Reservation findReservationDetailsById(@Param("id") Long id);

    /**
     * 查询用户在指定时间段内的预约信息
     */
    List<Reservation> findUserReservationsInPeriod(
            @Param("userId") Long userId,
            @Param("startTime") LocalTime startTime,
            @Param("endTime") LocalTime endTime,
            @Param("status") String status);

    /**
     * 查询班车的所有预约信息，按用户类型排序（教师优先）
     */
    List<Reservation> findScheduleReservationsOrderByUserType(@Param("scheduleId") Long scheduleId);

    /**
     * 获取班车预约统计信息
     */
    Map<String, Object> getScheduleReservationStats(@Param("scheduleId") Long scheduleId);
} 